package com.zhentao.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.mapper.DoctorInfoMapper;
import com.zhentao.pojo.DoctorInfo;
import com.zhentao.service.DoctorInfoService;
import com.zhentao.vo.Query;
import com.zhentao.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/doctorinfo")
public class DoctorInfoController {
    @Autowired
    private DoctorInfoMapper doctorInfoMapper;
    @Autowired
    private DoctorInfoService doctorInfoService;
    @RequestMapping("findPage1")
    public Result findPage1(@RequestBody(required = false) Query query){
        // 如果query为null，创建一个默认的查询对象
        if (query == null) {
            query = new Query();
        }
        Page<DoctorInfo> page = doctorInfoService.findPage(query);
        return Result.OK(page);
    }
    //查询医生信息


    //添加医生信息
    @RequestMapping("/addDoctor")
    public String addDoctor(@RequestBody(required = false) DoctorInfo doctorInfo){
        if (doctorInfo == null) {
            return "医生信息不能为空";
        }
        doctorInfo.setCreateTime(new Date());
        int insert = doctorInfoMapper.insert(doctorInfo);
        return insert>0?"添加成功":"添加失败";
    }

    //删除医生信息
    @RequestMapping("/deleteDoctor")
    public String deleteDoctor(Integer id){
        if (id == null) {
            return "ID不能为空";
        }
        int delete = doctorInfoMapper.deleteById(id);
        return delete>0?"删除成功":"删除失败";
    }

    //修改医生信息
    @PostMapping("/updateDoctor")
    public String updateDoctor(@RequestBody DoctorInfo doctor){
        if (doctor == null || doctor.getId() == null) {
            return "医生信息或ID不能为空";
        }
        int update = doctorInfoMapper.updateById(doctor);
        return update>0?"修改成功":"修改失败";
    }


}
